<template>
  <div class="my-container">
    <!--用户头像，编辑资料，用户的粉丝等-->
    <div v-if="user" class="header user-info">
      <!--      用户的头像跟编辑资料-->
      <div class="base-info">
        <div class="left">

          <van-image
            class="avater"
            fit="cover"
            round
            src="https://img01.yzcdn.cn/vant/cat.jpeg"
          />
          <span class="name">{{ userInfo.name }}</span>
        </div>

        <div class="right">
          <van-button size="mini" round>编辑资料</van-button>


        </div>
      </div>
      <!--      用户的粉丝关注等展示-->
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>

        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">获赞</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">关注</span>
        </div>



      </div>
    </div>
<!--    登录按钮部门-->
    <div v-else class="header no-login">
      <div class="login-btn">
        <img
          class="mobile-img"
          src="~@/assets/mobile.png"
          @click="$router.push('/login')" />
        <span class="text">登录 / 注册</span>
      </div>

    </div>


<!--功能导航-->
    <van-grid :column-num="2"  class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span  slot="text" class="text">收藏</span>
      </van-grid-item>

      <van-grid-item  class="grid-item">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span   slot="text"  class="text">历史</span>
      </van-grid-item>
    </van-grid>


    <van-cell title="消息通知" is-link url="" />
    <van-cell title="系统设置" class="setting-cell" is-link url="" />
    <van-cell v-if="user" title="退出登录" class="logout-cell" clickable @click="logout" />

  </div>
</template>

<script>
import  { mapState } from 'vuex'
import {getUserInfo} from '@/api/user'

export default {
  name: 'MyIndex',
  components: {},
  props: {},
  data () {
    return {
      userInfo:{}
    }
  },
  computed: {
    ...mapState(['user'])

  },
  watch: {},
  created () {
    if (this.user){

      this.getUserInfo()
    }

  },
  mounted () {},
  methods: {
    //退出登录
    logout(){
      // 退出提示
      // 在组件中需要使用 this.$dialog 来调用弹框组件
      this.$dialog.confirm({
        title: '确认退出吗？'
      }).then(() => {
        // on confirm
        // 确认退出：清除登录状态（容器中的 user + 本地存储中的 user）
        this.$store.commit('setUser', null)
      }).catch(() => {
        // on cancel
        console.log('取消执行这里')
      })

    },

    //获取登录的用户信息
   async getUserInfo(){
      try {
       const { data } = await getUserInfo()
        this.userInfo=data.data
      }catch (err){
        console.log(err)
        this.$toast('获取数据失败')
      }
    }

  }
}
</script>

<style scoped lang="less">
.my-container {
  > .header {
    height: 361px;
    background: url("~@/assets/banner.png") no-repeat;
    background-size: cover;

  }
  .no-login{
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .login-btn{

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile-img {
      display: flex;
      width: 200px;
      height: 200px;
    }
    .text{

      margin-top:20px ;
      color: whitesmoke;
    }

  }

  .user-info{

    .base-info{
      box-sizing: border-box;
      height: 231px;
      //background-color: pink;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      .left{
        display: flex;
        align-items: center;
        .avater{
          width: 132px;
          height: 132px;
          margin-right: 32px;
          border: 1px solid #fff;

        }
        .name{

          color: white ;
        }
      }
    }
    .data-stats{
      height: 130px;
      //background-color: red;
      display: flex;

      .data-item{
         flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      color: white;
        .count{
          font-size: 36px;
        }
        .text{
          margin-top: 10px;
          font-size: 23px;
        }

      }
    }


  }

  .grid-nav{
    .grid-item{
      i.iconfont{

        font-size: 45px;
      }
      i.icon-shoucang{
        color: #ce8483;
      }
      i.icon-lishi{
       color: #ff9d1d;
      }
      span.text{
        font-size: 28px;
      }


    }

  }
  .logout-cell{
    margin-top: 20px;
    text-align: center;
    color: #d86262;
  }




}
</style>
